import { createStore } from "vuex";
import commonApi from '@/assets/common';

export default createStore({
    // 声明变量
    state: {
        //授权菜单
        menuData: [],
        //全部-字典数据
        configAll: [],
        //启用状态-字典数据
        configEnable: [],
        //字典数据-级联
        typeObj: [],
        //部门
        depData: [],
        //角色
        groupData: [],
        //用户
        userData: [],
        //开票状态
        invoiceStatus: [
            {value: 1, label: '未开票'},
            {value: 2, label: '申请中'},
            {value: 3, label: '已开票'},
        ],
        //案件类型
        caseType: [
            {value: 'type_nonlawsuit', label: '非诉类型'},
            {value: 'type_penal', label: '刑事案件'},
            {value: 'type_lawsuit', label: '诉讼类型'},
        ],
        //开庭状态
        confirm_status: [
            {value: 0, label: '待开庭'},
            {value: 1, label: '已开庭'},
            {value: 2, label: '已取消'},
        ],
        //开票状态
        invoice_status: [
            {value: 1, label: '未开票'},
            {value: 2, label: '申请中'},
            {value: 3, label: '已开票'},
        ],
        //到账状态
        coll_status: [
            {value: 1, label: '未到账', color: '#F56C6C'},
            {value: 2, label: '部分到账', color: '#E6A23C'},
            {value: 3, label: '全额到账', color: '#67C23A'}, 
        ],
        //到账状态
        approval_status: [
            {value: 1, label: '待审批'},
            {value: 2, label: '审批通过'},
            {value: 3, label: '审批拒绝'},
        ],
    },

    // 修改变量（state不能直接赋值修改，只能通过mutations）
    mutations: {
        //获取授权菜单
        getMenu(state){
            commonApi.getMenu()
            .then(res=> {
                state.menuData = res.menus_tree;
                localStorage.setItem('menuData', JSON.stringify(res.menus_tree));
            })
        },

        //获取数据字典
        getConfigData(state){
            commonApi.getConfigData()
            .then(res=> {
                //拼接区分字典类型数组
                let array = res.list;
                let result = {};
                let openResult = {};
                array.forEach(item => {
                    //全部
                    if (result.hasOwnProperty(item.type)) {                        
                        result[item.type] = [...result[item.type], ...[item]];
                    } else {
                        result[item.type] = [item];
                    }
                    //启用状态
                    if(item.status == 1){
                        if (openResult.hasOwnProperty(item.type)) {                        
                            openResult[item.type] = [...openResult[item.type], ...[item]];
                        } else {
                            openResult[item.type] = [item];
                        }
                    }
                });
                //全部
                state.configAll = result;
                //启用状态
                state.configEnable = openResult;
                //拼接级联数据
                let type_nonlawsuit = result.type_nonlawsuit;
                let type_penal = result.type_penal;
                let type_lawsuit = result.type_lawsuit;
                let obj = [
                    {name: 'type_nonlawsuit',value: '非诉类型',children: type_nonlawsuit},
                    {name: 'type_penal',value: '刑事类型',children: type_penal},
                    {name: 'type_lawsuit',value: '诉讼类型',children: type_lawsuit},
                ];
                state.typeObj = obj;
            })
        },

        //获取部门
        getDep(state){
            commonApi.getDep()
            .then(res=> {
                state.depData = res;
            })
        },

        //获取角色
        getGroup(state){
            commonApi.getGroup()
            .then(res=> {
                if(res.list){
                    let arr = []
                    for(let key in res.list){
                        arr.push({value: res.list[key].id, label: res.list[key].name})
                    }
                    state.groupData = arr;
                }
            })
        },

        //获取部门
        getUser(state){
            commonApi.getUser()
            .then(res=> {
                state.userData = res.list;
            })
        },
    },

    // mutations的值由actions传入
    actions: {
        
    },
    
    modules: {
        
    },
})